package Task_02;

import java.util.Scanner;

public class ChessGame {
    private String[][] board; //定义棋盘
    private char white; //白棋
    private char black; //黑棋
    private int rows; //绘画棋盘
    private int cols;

    ChessGame() {      //无参构造方法
    }

    ChessGame(int rows, int cols, char white, char black) {     //有参构造方法
        setBoard(rows, cols);
        setWhite(white);
        setBlack(black);
        setRows(rows);
        setCols(cols);
    }
        //棋盘行
    public void setRows(int rows) {
        this.rows = rows;
    }
        //棋盘列
    public void setCols(int cols) {
        this.cols = cols;
    }
        //获取棋盘行
    public int getRows() {
        return rows;
    }
        //获取棋盘列
    public int getCols() {
        return cols;
    }
        //设置棋盘
    public void setBoard(int rows, int cols) {
        board = new String[rows][cols];
    }
        //设置白起
    public void setWhite(char white) {
        this.white = white;
    }
        //设置黑棋
    public void setBlack(char black) {
        this.black = black;
    }
        //返回棋盘的数组
    public String[][] getBoard(int rows, int cols) {
        return board;
    }
        //获取自定义白起
    public char getWhite() {
        return white;
    }
        //获取自定义黑棋
    public char getBlack() {
        return black;
    }
        //利用双重for循环绘制棋盘
    public void getBoard() {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (i == 0) {
                    board[i][j] = String.format("%x", j - 1);
                } else if (j == 0) {
                    board[i][j] = String.format("%x", i - 1);
                } else {
                    board[i][j] = "+";
                }
            }
        }
        board[0][0] = " ";
    }
        //打印棋盘
    public void showBoard() {
        for (String[] str : board) {
            for (int j = 0; j < board[0].length; j++) {
                if ("白".equals(str[j])) {
                    System.out.print(white + " ");
                } else if ("黑".equals(str[j])) {
                    System.out.print(black + " ");
                } else {
                    System.out.print(str[j] + " ");

                }
            }
            System.out.println();
        }
    }
            //判断黑棋和白棋的输赢
    private Boolean isWin(int x, int y, String chess, int rows, int cols) {
        int countCol = -1;
        int countRow = -1;
        int countDiagonalLeft = -1;
        int countDiagonalRight = -1;
        int a;
        int b;
        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countRow++;
                if (b == 0) {
                    break;
                }
                b--;
            } else {
                break;
            }
        }
        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countRow++;
                if (b == (cols - 1)) {
                    break;
                }
                b++;
            } else {
                break;
            }
        }

        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countCol++;
                if (a == 0) {
                    break;
                }
                a--;
            } else {
                break;
            }
        }
        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countCol++;
                if (a == (rows - 1)) {
                    break;
                }
                a++;
            } else {
                break;
            }
        }
        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countDiagonalLeft++;
                if (a == 0 || b == 0) {
                    break;
                }
                a--;
                b--;
            } else {
                break;
            }
        }
        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countDiagonalLeft++;
                if (a == (rows - 1) || b == (cols - 1)) {
                    break;
                }
                a++;
                b++;
            } else {
                break;
            }
        }
        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countDiagonalRight++;
                if (a == 0 || b == (cols - 1)) {
                    break;
                }
                a--;
                b++;
            } else {
                break;
            }
        }
        a = x;
        b = y;
        while (true) {
            if (chess.equals(board[a][b])) {
                countDiagonalRight++;
                if (a == (rows - 1) || b == 0) {
                    break;
                }
                a++;
                b--;
            } else {
                break;
            }
        }

        System.out.println("countRow:" + countRow);
        System.out.println("countCol:" + countCol);
        System.out.println("countDiagonalLeft:" + countDiagonalLeft);
        System.out.println("countDiagonalRight:" + countDiagonalRight);

        return (countRow >= 5 || countCol >= 5 || countDiagonalLeft >= 5 || countDiagonalRight >= 5);
    }
        //游戏开始,使用while循环判断胜负
    public void startGame() {
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        int x;
        int y;
        String chess;
        while (true) {
            if (flag) {
                System.out.println("白棋落子");
                chess = "白";
            } else {
                System.out.println("黑棋落子");
                chess = "黑";
            }
            x = sc.nextInt() + 1;
            y = sc.nextInt() + 1;
            if (x < 0 || y < 0 || x > 16 || y > 16) {
                System.out.println("超出棋盘，请仔细考虑再落子");
                continue;
            }
            if ("+".equals(board[x][y])) {
                board[x][y] = chess;
            } else {
                System.out.println("当前坐标不可用，重新考虑");
                continue;
            }
            showBoard();
            flag = !flag;
            boolean isWin = isWin(x, y, chess, getRows(), getCols());
            if (isWin) {
                System.out.printf("恭喜少侠" + chess + "棋获胜");
                break;
            }
        }
        sc.close();
    }

}
